package com.nanjia.boot.outboundorderview.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.myflow.order.converter.OrderConverter;
import com.nanjia.boot.myflow.order.mapper.OrderMapper;
import com.nanjia.boot.myflow.order.model.entity.Order;
import com.nanjia.boot.myflow.order.model.vo.OrderVO;
import com.nanjia.boot.outboundorder.mapper.OutboundOrderMapper;
import com.nanjia.boot.outboundorderview.converter.OutboundOrderViewConverter;
import com.nanjia.boot.outboundorderview.mapper.OutboundOrderViewMapper;
import com.nanjia.boot.outboundorderview.model.entity.OutboundOrderView;
import com.nanjia.boot.outboundorderview.model.form.OutboundOrderViewForm;
import com.nanjia.boot.outboundorderview.model.query.OutboundOrderViewQuery;
import com.nanjia.boot.outboundorderview.model.vo.OutboundOrderViewVO;
import com.nanjia.boot.outboundorderview.service.OutboundOrderViewService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 出库单业务关联服务实现类
 *
 * @author 周洋
 * @since 2025-08-25 00:11
 */
@Service
@RequiredArgsConstructor
public class OutboundOrderViewServiceImpl extends ServiceImpl<OutboundOrderViewMapper, OutboundOrderView> implements OutboundOrderViewService {

    private final OutboundOrderViewConverter outboundOrderViewConverter;

    private final OrderConverter orderConverter;

    private final OutboundOrderViewMapper outboundOrderViewMapper;

    private final OutboundOrderMapper outboundOrderMapper;

    private final OrderMapper orderMapper;
    /**
    * 获取出库单业务关联分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<OutboundOrderViewVO>} 出库单业务关联分页列表
    */
    @Override
    public IPage<OutboundOrderViewVO> getOutboundOrderViewPage(OutboundOrderViewQuery queryParams) {
        Page<OutboundOrderViewVO> pageVO = this.baseMapper.getOutboundOrderViewPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取出库单业务关联表单数据
     *
     * @param id 出库单业务关联ID
     * @return 出库单业务关联表单数据
     */
    @Override
    public OutboundOrderViewForm getOutboundOrderViewFormData(Long id) {
        OutboundOrderView entity = this.getById(id);
        return outboundOrderViewConverter.toForm(entity);
    }
    
    /**
     * 新增出库单业务关联
     *
     * @param formData 出库单业务关联表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveOutboundOrderView(OutboundOrderViewForm formData) {
        OutboundOrderView entity = outboundOrderViewConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新出库单业务关联
     *
     * @param id   出库单业务关联ID
     * @param formData 出库单业务关联表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateOutboundOrderView(Long id,OutboundOrderViewForm formData) {
        OutboundOrderView entity = outboundOrderViewConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除出库单业务关联
     *
     * @param ids 出库单业务关联ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteOutboundOrderViews(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的出库单业务关联数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public Result<Object> getOrder(OutboundOrderViewQuery queryParams) {
        if (queryParams != null && queryParams.getOutboundOrderId() != null) {
            List<OutboundOrderView> list = this.list(new LambdaQueryWrapper<OutboundOrderView>().eq(OutboundOrderView::getOutboundOrderId, queryParams.getOutboundOrderId()));
            if (list != null && list.size() > 0) {
                OutboundOrderView vo = list.get(0);
                if (vo.getExternalType().equals("销售")) {
                    Order order = orderMapper.selectById(vo.getExternalId());
                    OrderVO vo1 = orderConverter.toVO(order);
                    return Result.success(vo1);
                }
            }
        }
        return null;
    }
}
